﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Navigation;
using GalaSoft.MvvmLight.Command;

namespace GurLoader.ViewModel
{
    public class LoginViewModel
    {
        public event EventHandler<UriPropertyChangedEventArgs> CurrentUriChanged;

        private Uri currentUri = null;

        public LoginViewModel(Uri loginUri)
        {
            if ( loginUri == null )
                throw new ArgumentNullException("loginUri");

            CurrentUri = loginUri;
        }

        public Uri CurrentUri
        {
            get { return currentUri; }
            private set
            {
                currentUri = value;
                FireCurrentUriChanged(currentUri);
            }
        }

        public RelayCommand<object> NavigatedCommnad
        {
            get
            {
                return new RelayCommand<object>(o =>
                {
                    Uri uri = o as Uri;
                    if (uri == null)
                    {
                        NavigationEventArgs args = o as NavigationEventArgs;
                        if (args != null)
                            uri = args.Uri;
                    }

                    if (uri != null)
                        CurrentUri = uri;
                });
            }

        }

        private void FireCurrentUriChanged(Uri uri)
        {
            EventHandler<UriPropertyChangedEventArgs> handler = CurrentUriChanged;
            if (handler != null)
                handler(this, new UriPropertyChangedEventArgs(uri));
        }
    }
}
